
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
//import com.badlogic.gdxinvaders.screens.GameLoop;
//import com.badlogic.gdxinvaders.screens.GameOver;
//import com.badlogic.gdxinvaders.screens.MainMenu;
import com.badlogic.gdx.graphics.GL10;




public class SkeletonApp implements ApplicationListener {
	/** flag indicating whether we were initialized already **/
	private boolean isInitialized =false;

	/** the current screen **/
	private ScreenState m_screenState;
	// state id
	private int m_currentState = GameState.STATE_SPLASH;
	long lastRender = System.nanoTime();

	@Override
	public void dispose () {

	}

	@Override
	public void render () {
		Application app = Gdx.app;
		// update the screen
		m_screenState.update(app);
		// render the screen
		m_screenState.render(app);
		
		
		long now = System.nanoTime();
		
		if (now - lastRender < 30000000) { // 30 ms, ~33FPS
			try {
				Thread.sleep(30 - (now - lastRender) / 1000000);
			} catch (InterruptedException e) {
			}
		}
		
		

		lastRender = now;

		// when the screen is done we change to the
		// next screen
		if (m_screenState.isDone()) {
			
			// dispose the current screen
			int nextstate = m_screenState.getNextState();
			System.out.println("------------------change state----------- to: "+nextstate);
			m_screenState.dispose();

			if(nextstate >= GameState.STATE_GP){
				nextstate = nextstate - GameState.STATE_GP;
				m_screenState = new ScreenStateGameplay(Gdx.app,nextstate + 1);
			}
			else {
				switch(nextstate)
				{
					case GameState.STATE_SPLASH:
						m_screenState = new ScreenStateSplash(Gdx.app);
						break;
					case GameState.STATE_MM:
						m_screenState = new ScreenStateMM(Gdx.app);
						break;
					case GameState.STATE_MAP:
						m_screenState = new ScreenStateMap(Gdx.app);
						break;
				}
			}
		}

	}

	@Override
	public void resize (int width, int height) {

	}

	@Override
	public void create () {
		
		if (!isInitialized) {
			m_screenState = new ScreenStateSplash(Gdx.app);
			//Music music = Gdx.audio.newMusic(Gdx.files.getFileHandle("data/8.12.mp3", FileType.Internal));
			//music.setLooping(true);
			//music.play();
			isInitialized = true;
		}
	}

	@Override
	public void pause () {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume () {
		// TODO Auto-generated method stub
		System.out.println("resume");
	}
}
